﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Manage.Controller;
using GtsServer.Model;
using GtsServer.Common;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// TaskUserAjax 的摘要说明
    /// </summary>
    public class TaskUserAjax : IHttpHandler
    {
        TaskUserController Controller = new TaskUserController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    //case "Select":
                    //    GetListByIMSI(context);
                    //    break;
                    default:
                        break;
                }
            }
        }

        private void Add(HttpContext context)
        {
            int taskID = Convert.ToInt32(context.Request["taskID"]);
            int userID = Convert.ToInt32(context.Request["userID"]);
            TaskUser model = new TaskUser()
            {
                TaskID = taskID,
                UserID = userID,
                GameCount = 0,
                IsHandEdit = false,
                JoinTime = DateTime.Now,
                Score = 0,
                State = 2,
                MatchGold = 0
            };
            context.Response.Write(Controller.Add(model));
        }

        private void Update(HttpContext context)
        {
            int Score = Convert.ToInt32(context.Request["Score"]);
            int MatchGold = Convert.ToInt32(context.Request["MatchGold"]);
            int id = Convert.ToInt32(context.Request["id"]);
            TaskUser model = new TaskUser()
            {
                ID = id,
                Score = Score,
                MatchGold = MatchGold,
                IsHandEdit = true
            };
            context.Response.Write(Controller.Update(model));
        }
        private void GetList(HttpContext context)
        {
            int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
            int pageSize = Convert.ToInt32(context.Request["pageSize"]);
            int count = 0;
            int position = 0;
            string taskID = context.Request["typeID"] ?? "";
            string sysAccount = context.Request["sysAccount"] ?? "";
            List<TaskUserShow> list = Controller.GetList(taskID, sysAccount, pageIndex, pageSize, out count, out position);

            string json = JsonHelper.ListToJson<TaskUserShow>(list, count.ToString() + "\",\"pos\":\"" + position);
            context.Response.Write(json);
        }
        //private void GetListByIMSI(HttpContext context)
        //{
        //    string IMSI = context.Request["IMSI"];
        //    List<TaskUserShow> list = Controller.GetListByIMSI(IMSI);
        //    string json = JsonHelper.ListToJson<TaskUserShow>(list);
        //    context.Response.Write(json);
        //}
        private void Delete(HttpContext context)
        {
            int id = Int32.Parse(context.Request["id"]);
            context.Response.Write(Controller.Delete(id));
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}